/**
 *
 */
package com.angel.common.helpers;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/** The <code>CollectionHelper.class</code> helps you to manage some collections functions.
 *  All its methods are statics.
 *
 * @author William
 * @version
 */
public class CompareHelper {

	/** It is the logger for the class. */
	private static Logger logger = Logger.getLogger(CompareHelper.class);

	private CompareHelper(){
		super();
	}

	/** Set current log level.
	 *
	 * @param logLevel
	 */
	public static void setLogLevel(Level logLevel){
		logger.setLevel(logLevel);
	}

	public static int compareObject(Comparable<Object> object1, Comparable<Object> object2){
		if(object1 == null){
			return 1;
		}
		if(object2 == null){
			return -1;
		}
		int result = -2;
		if(object1.compareTo(object2) == object2.compareTo(object1)){
			result = object2.compareTo(object1);
		}
		return result;
	}

	public static int[] compareEachObject(Comparable<Object>[] objects1, Comparable<Object>[] objects2){
		int results[] = null;
		if(objects1 != null && objects2 != null){
			if(objects1.length == objects2.length){
				results = new int[objects1.length];
			} else {
				int minLength = (objects2.length > objects1.length) ? objects1.length: objects2.length;
				results = new int[minLength];
				for(int i = 0; i < minLength; i++){
					results[i] = objects1[i].compareTo(objects2[i]);
				}
			}
		} else {
			results = new int[1];
			results[0] = -2;
		}
		return results;
	}

	public static boolean isBetweenComparableObjects(Comparable<Object> object, Comparable<Object> minimun, Comparable<Object> maximun){
		boolean isBetweenObjects = false;
		if(object == null || minimun == null || maximun == null){
			isBetweenObjects = false;
			return isBetweenObjects;
		}
		isBetweenObjects = (minimun.compareTo(object) >= 0 && minimun.compareTo(object) <= 0);
		return isBetweenObjects;
	}

	public static boolean isOverlappedRangeBetweenComparableObjects(Comparable<Object> minimunObject, Comparable<Object> maximunObject, Comparable<Object> minimun, Comparable<Object> maximun){
		boolean isOverlappedRange = false;
		if(maximunObject == null || minimunObject == null|| minimun == null || maximun == null){
			isOverlappedRange = false;
			return isOverlappedRange;
		}
		isOverlappedRange = (minimun.compareTo(minimunObject) >= 0 && minimun.compareTo(maximunObject) <= 0) ||
				(maximun.compareTo(minimunObject) >= 0 && maximun.compareTo(maximunObject) <= 0);
		return isOverlappedRange;
	}
}
